import csv
import matplotlib.pyplot as plt
import matplotlib
from datetime import datetime
plt.style.use('seaborn')
matplotlib.rc("font", family='Microsoft YaHei')
from matplotlib.font_manager import FontManager

# mpl_fonts = set(f.name for f in FontManager().ttflist)
#
# print('all font list get from matplotlib.font_manager:')
# for f in sorted(mpl_fonts):
#     print('\t' + f)
# mpl.rcParams['font.sans-serif'] = ['KaiTi', 'SimHei', 'FangSong']  # 汉字字体,优先使用楷体，如果找不到楷体，则使用黑体
# mpl.rcParams['axes.unicode_minus'] = False  # 正常显示负号

# print(matplotlib.matplotlib_fname())

filename = r'D:\ui\file_data\data\sitka_weather_2018_simple.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

# for index,column_header in enumerate(header_row):
#     print(index,column_header)

# 从文件中获取日期和最高温度
    dates,highs = [],[]
    for row in reader:
        current_date = datetime.strptime(row[2],'%Y-%m-%d')
        # current_date = row[2]
        high = int(row[5])
        dates.append(current_date)
        highs.append(high)

# 根据最高温度绘制图形
# plt.style.use('seaborn')  # 有网格就无法中文显示 要放在字体设置前面
fig,ax = plt.subplots()
ax.plot(dates,highs,c='red')

# 设置图形的格式
ax.set_title("2018年每日最高温度",fontsize=18)
ax.set_xlabel("",fontsize=16)
fig.autofmt_xdate()  # 绘制倾斜的日期标签，避免彼此重叠
ax.set_ylabel("温度(F)",size=16)
ax.tick_params(axis='both',which='major',labelsize=16)

plt.show()



